-- TalkTemplateM
-- Create by panyl Jun/23/2015
-- 说话模板模块

module("TalkTemplateM", package.seeall);

local talkMap = {};
local talks = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    talkMap = {};
    talks = {};

    -- 转换talk_template_CSV表
    local talk_template_CSV = get_talk_template_CSV();
    for i = 1, #talk_template_CSV do
        local row = talk_template_CSV[i];

        talkMap[row.id] = row;

        talks[row.type] = talks[row.type] or {};
        talks[row.type][row.group] = talks[row.type][row.group] or {};
        table.insert(talks[row.type][row.group], row);
    end

    talk_template_CSV = {};
end

-- 模块初始化
function init()
    loadCsv();
end

function query(id, path)
    local m = talkMap[id];

    if not m or not path then
        return m;
    end

    return m[path];
end

-- 获取怪物发言内容
function getTalkId(gridType, group)
    group = group or 1;

    if not talks[gridType] then
        return;
    end

    -- 随机获取发言内容
    local talkList = table.values(talks[gridType][group]);

    local index = math.random(#talkList);

    return talkList[index].id;
end
